package com.keymozy.controller;

import com.keymozy.pojo.Model;
import com.keymozy.pojo.Store;
import com.keymozy.proxy.Right;
import com.keymozy.service.ModelService;
import com.keymozy.state.Code;
import com.keymozy.state.RespData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/model")
@CrossOrigin
public class ModelController {
    @Autowired
    private ModelService modelService;

    @GetMapping("/getAll")
    @Right(power = "3")
    public RespData getAll(int currentPage,int pageSize,Model model){
        Map<String, Object> result = modelService.getAll(currentPage, pageSize, model);
        log.info("res:"+result);
        if (null == result)
            return new RespData(Code.FALSE_SEARCH,null);
        return new RespData(Code.SUCCESS_SEARCH,result);
    }

    @PostMapping("/add")
    @Right(power = "3")
    public RespData add(@RequestBody Model model){
        log.info("model:"+model);
        boolean flag = modelService.add(model);
        if (flag)
            return new RespData(Code.SUCCESS_ADD_1,model);
        return new RespData(Code.FALSE_ADD,null);
    }

    @GetMapping("/getStores")
    public RespData getStores(){
        List<Store> store = modelService.getStore();
        if (null==store || store.size()==0)
            return new RespData(Code.FALSE_SEARCH,null);
        return  new RespData(Code.SUCCESS_SEARCH,store);
    }

    @PostMapping("/addNum")
    @Right(power = "3")
    public RespData addNum(@RequestBody Model model){
        boolean flag = modelService.addNum(model);
        if (flag)
            return new RespData(Code.SUCCESS_ADD_1,model);
        else
            return new RespData(Code.FALSE_ADD,null);
    }

    @PostMapping("/update")
    @Right(power = "3")
    public RespData alter(@RequestBody Model model){
        boolean flag = modelService.alterModel(model);
        if (flag)
            return new RespData(Code.SUCCESS_UPDATE_1,model);
        else
            return new RespData(Code.FALSE_UPDATE,null);
    }

    @PostMapping("/delete")
    @Right(power = "3")
    public RespData delete(@RequestBody Model[] models){
        if (modelService.deleteModel(models))
            return new RespData(Code.SUCCESS_DELETE_1,null);
        return new RespData(Code.FALSE_DELETE,null);
    }
    @GetMapping("/zone")
    public RespData echarts(){
         return new RespData(Code.SUCCESS_SEARCH,modelService.echartsZone());
    }
}
